package views.searchViews;

import javax.swing.JFrame;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import controllers.searchControllers.ZoekPersoneelController;
import core.models.dataModels.searchModels.ZoekPersoneelModel;
import core.models.domainModel.Eigenaar;
import core.models.domainModel.Personeel;
import core.models.domainModel.PersoneelsType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ZoekPersoneelView extends JFrame{
	private static final long serialVersionUID = 1L;
	private JTextField txtPersoneelsNummer;
	private JTextField txtVoornaam;
	private JTextField txtFamilienaam;
	
	private ZoekPersoneelModel _model;
	
	public ZoekPersoneelView(ZoekPersoneelModel model) {

		
		_model = model;
		
		final JComboBox<Eigenaar> cmbEigenaar = new JComboBox<Eigenaar>();
		
		for(Eigenaar dit : _model.getMogelijkeEigenaars()){
			cmbEigenaar.addItem(dit);
		}
		
		
		JLabel lblEigenaar = new JLabel("Eigenaar");
		
		JLabel lblPersoneelstype = new JLabel("PersoneelsType:");
		
		final JComboBox<PersoneelsType> cmbPersoneelsType = new JComboBox<PersoneelsType>();
		
		for(PersoneelsType dit : _model.getMogelijkePersoneelsType()){
			cmbPersoneelsType.addItem(dit);
		}
		
		JLabel lblPersoneelsnummer = new JLabel("PersoneelsNummer");
		
		JLabel lblVoornaam = new JLabel("Voornaam");
		
		JLabel lblNaam = new JLabel("Naam");
		
		txtPersoneelsNummer = new JTextField();
		txtPersoneelsNummer.setColumns(10);
		if(model.getPersoneelsnummer() != null && model.getPersoneelsnummer() != ""){
			txtPersoneelsNummer.setText(model.getPersoneelsnummer());
		}
		
		txtVoornaam = new JTextField();
		txtVoornaam.setColumns(10);
		if(model.getVoornaam() != null && model.getVoornaam() != ""){
			txtVoornaam.setText(model.getVoornaam());
		}
		
		txtFamilienaam = new JTextField();
		txtFamilienaam.setColumns(10);
		if(model.getFamNaam() != null && model.getFamNaam() != ""){
			txtFamilienaam.setText(model.getFamNaam());
		}
		
		
		JList list = new JList();
		list.setLayoutOrientation(JList.VERTICAL);
		list.setFixedCellWidth(200); 
		list.setVisibleRowCount(-1);
		list.setSelectedIndex(0);
		
		JScrollPane listScroller = new JScrollPane(list);
		DefaultListModel<Personeel> lstPersoneel = new DefaultListModel<Personeel>();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if(model.getPersoneelslijst() != null &&  model.getPersoneelslijst().size()>0){
			for(Personeel item : model.getPersoneelslijst()){
				lstPersoneel.addElement(item);
			}
		}
		list.setModel(lstPersoneel);

		
		JButton btnZoeken = new JButton("Zoeken");
		btnZoeken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				_model.setSelectedEigenaar((Eigenaar)cmbEigenaar.getSelectedItem());
				_model.setSelectedPersoneelsType((PersoneelsType) cmbPersoneelsType.getSelectedItem());
				_model.setPersoneelsnummer(txtPersoneelsNummer.getText());
				_model.setVoornaam(txtVoornaam.getText());
				_model.setFamNaam(txtFamilienaam.getText());
				ZoekPersoneelController.handleView(_model);
			}
		});
		
		JList list_1 = new JList();
		
		JLabel label = new JLabel("");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(40)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label)
						.addComponent(listScroller, GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblEigenaar, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblPersoneelstype, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))
							.addGap(32)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(btnZoeken, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(cmbPersoneelsType, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(cmbEigenaar, 0, 157, Short.MAX_VALUE))
							.addGap(56)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblPersoneelsnummer, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtPersoneelsNummer, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblVoornaam, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNaam, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtFamilienaam, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(36)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblPersoneelsnummer)
								.addComponent(txtPersoneelsNummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblVoornaam)
								.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNaam)
								.addComponent(txtFamilienaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblEigenaar)
								.addComponent(cmbEigenaar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblPersoneelstype)
								.addComponent(cmbPersoneelsType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addComponent(btnZoeken)))
					.addGap(40)
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(listScroller, GroupLayout.PREFERRED_SIZE, 204, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JLabel lblE = new JLabel("<html><table width = '500px' style ='background-color:#DAA520 ;'><tr><td width = '100px'>PersoneelsNummer</td> <td width = '100px'>PersoneelsType</td><td width = '150px'>Voornaam</td><td width = '150px'>Naam</td></tr></table></html>");
		listScroller.setColumnHeaderView(lblE);
		getContentPane().setLayout(groupLayout);
	}
}